package cn.piggy.quartz.handler;


import cn.piggy.model.JobInfo;
import cn.piggy.quartz.listener.MyJobListener;
import org.quartz.*;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Objects;


@Component
public class JobHandler {

    @Resource
    private Scheduler scheduler;

    public void addJob(JobInfo jobInfo) throws SchedulerException {
        Objects.requireNonNull(jobInfo, "任务信息不能为空");

        // 生成job key
        JobKey jobKey = JobKey.jobKey(jobInfo.getJobName(), jobInfo.getJobGroup());
        // 当前任务不存在才进行添加
        if (!scheduler.checkExists(jobKey)) {
            // 任务明细
            JobDetail jobDetail = JobBuilder
                    .newJob(jobInfo.getClassType())
                    .withIdentity(jobKey)
                    .withIdentity(jobInfo.getJobName(), jobInfo.getJobGroup()).storeDurably().requestRecovery()
                    .requestRecovery(true)
                    .withDescription(jobInfo.getJobName())
                    .build();
            // 配置信息
            jobDetail.getJobDataMap().put("config", jobInfo.getConfig());
            // 定义触发器
            TriggerKey triggerKey = TriggerKey.triggerKey(jobInfo.getTriggerName(), jobInfo.getTriggerGroup());
            // 设置任务的错过机制
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity(triggerKey)
                    .withSchedule(CronScheduleBuilder.cronSchedule(jobInfo.getCron())
                            .withMisfireHandlingInstructionFireAndProceed())
                    .build();
            scheduler.getListenerManager().addJobListener(new MyJobListener());
            scheduler.scheduleJob(jobDetail, trigger);
//            scheduler.rescheduleJob()
        } else {
            throw new SchedulerException(jobInfo.getJobName() + "任务已存在，无需重复添加");
        }
    }
    public void pauseJob(String jobGroup, String jobName) throws SchedulerException {
        JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
        if (scheduler.checkExists(jobKey)) {
            scheduler.pauseJob(jobKey);
        }
    }

    /**
     * 继续任务
     */
    public void continueJob(String jobGroup, String jobName) throws SchedulerException {
        JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
        if (scheduler.checkExists(jobKey)) {
            scheduler.resumeJob(jobKey);
        }
    }

    /**
     * 删除任务
     */
    public boolean deleteJob(String jobGroup, String jobName) throws SchedulerException {
        JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
        if (scheduler.checkExists(jobKey)) {
            // 这里还需要先删除trigger相关
            //TriggerKey triggerKey = TriggerKey.triggerKey(jobInfo.getTriggerName(), jobInfo.getTriggerGroup());
            //scheduler.getTrigger()
            //scheduler.rescheduleJob()
            return scheduler.deleteJob(jobKey);
        }
        return false;
    }

    private CronScheduleBuilder getCronScheduleBuilder(String policy, String cron){
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
        switch (policy){
            case "1":
                return cronScheduleBuilder;
            case "2":
                return cronScheduleBuilder.withMisfireHandlingInstructionFireAndProceed();
            case "3":
                return cronScheduleBuilder.withMisfireHandlingInstructionDoNothing();
            case "4":
                return cronScheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
            default:
                throw new RuntimeException();
        }
    }

}
